from sqlalchemy.orm import Session
from schema import Remote_Sensing_Image_schema
from model import Remote_Sensing_Image_model



def get_Remote_Sensing_Image(db: Session, ID: int):
    return db.query(Remote_Sensing_Image_model.Remote_sensing_image).filter(Remote_Sensing_Image_model.Remote_sensing_image.ID == ID).first()


def get_all_Remote_Sensing_Image(db: Session):
    return db.query(Remote_Sensing_Image_model.Remote_sensing_image).all()


def delete_Remote_Sensing_Image(db: Session, ID):
    to_delete = db.query(Remote_Sensing_Image_model.Remote_sensing_image).filter(Remote_Sensing_Image_model.Remote_sensing_image.ID == ID)
    deleted = to_delete.delete()
    if deleted == 0:
        return None
    else:
        db.commit()
        return True


def create_Remote_Sensing_Image(db: Session, Remote_Sensing_Image: Remote_Sensing_Image_schema.Remote_sensing_image):
    db_Remote_Sensing_Image = Remote_Sensing_Image_model.Remote_sensing_image(
        PlotID=Remote_Sensing_Image.PlotID,
        ImageURL=Remote_Sensing_Image.ImageURL,
        ImageType=Remote_Sensing_Image.ImageType,
        CaptureTime=Remote_Sensing_Image.CaptureTime,
        AnalysisResultJSON=Remote_Sensing_Image.AnalysisResultJSON,
        Remarks=Remote_Sensing_Image.Remarks,
    )
    db.add(db_Remote_Sensing_Image)
    db.commit()
    db.refresh(db_Remote_Sensing_Image)
    return db_Remote_Sensing_Image


def update_Remote_Sensing_Image(db: Session, ID, field_name, new_value):
    db_Remote_Sensing_Image = db.query(Remote_Sensing_Image_model.Remote_sensing_image).filter(Remote_Sensing_Image_model.Remote_sensing_image.ID == ID).first()
    setattr(db_Remote_Sensing_Image, field_name, new_value)
    db.commit()
    db.refresh(db_Remote_Sensing_Image)
    return db_Remote_Sensing_Image
